<?php

/**
 * Erra_Fields_MultipleCheckBox - Shows multiple checkbox
 *
 * @package
 * @author Alexis Eric
 * @copyright Copyright (c) 2012 - Erra Prod
 * @version $1.0$
 * @access public
 */
class Erra_Fields_Tree extends Erra_Fields_Base_Field {

    function __construct($objectId) {
        parent::__construct($objectId);
        $this->setDatabaseType('varchar', 256);
    }

    public function recursiveTreeAssignation($fieldData, $values) {
        foreach ($fieldData['children'] as $childNum => $child) {
            if (isset($child['children']) && count($child['children'])) {
                $fieldData['children'][$childNum] = $this->recursiveTreeAssignation($child, $values);
            }
            foreach ($values as $value) {
                if (isset($child['key']) && $child['key'] == $value) {
                    $fieldData['children'][$childNum]['select'] = true;
                }
            }
        }
        return $fieldData;
    }

    public function getTree($extraProperties) {
        $res = '';
        $fieldData = $this->fieldData;
        $data_array = $this->recursiveTreeAssignation($fieldData, $this->getValue());
        $javascript_array = htmlspecialchars(json_encode($data_array));
        $res .= '<div data-content="' . $this->getHtmlId() . '" class="dynatree-container" data-dyna="' . $javascript_array . '"> </div>';
        $res .= '<input type="hidden" name="' . $this->getHtmlName() . '" id="' . $this->getHtmlId() . '" value="' . $this->getRawData() . '" '.$this->getHtmlAttributes($extraProperties).' />';
        return $res;
    }

    /**
     * Erra_Fields_MultipleCheckBox::editInstance() Shows multiple checkboxes
     *
     * @param mixed $lang
     * @return
     */
    public function editInstance(Array $extraProperties = null) {
        $res = '<div class="erra-form-field-instance">';
        $res .= $this->outputFieldHeader();
        $res .= '<div>';
        $res .= $this->getTree($extraProperties);
        $res .= '</div>';
        $res .= '</div>';
        return $res;
    }

    /**
     * Erra_Fields_MultipleCheckBox::setRawData() - Sets raw data (may contains multiple values)
     *
     * @param mixed $value
     * @return
     */
    public function setRawData($value) {
        // Values from $_POST
        if (is_array($value)) {
            $this->_rawData = implode('§', $value);
            // Values from elsewhere
        } else {
            $this->_rawData = $value;
        }
    }

    /**
     * Erra_Fields_MultipleCheckBox::getValue() - Return array with values
     *
     * @return
     */
    public function getValue() {
        $data = $this->getRawData();
        if (strlen($data)) {
            if (strstr($data, '§') !== false) {
                return explode('§', $data);
            } else {
                return array($data);
            }
        }
        return array();
    }

    function toHtml($displayParameters = null) {
        return $this->getRawData();
    }

}